package it.fuesi.synotools.services.tasks.download;

import android.content.Context;
import android.util.Log;

import it.fuesi.synotools.activities.DownloadStationSettingsActivity;
import it.fuesi.synotools.model.download.DownloadStationClientParameters;
import it.fuesi.synotools.model.download.DownloadStationConfig;
import it.fuesi.synotools.model.download.DownloadStationConfigResponse;
import it.fuesi.synotools.services.client.DownloadStationClient;
import it.fuesi.synotools.services.client.ServiceClientFactory;
import it.fuesi.synotools.services.tasks.base.BaseTask;

/**
 * Created by pascotto on 17/10/14.
 */
public class DownloadConfigTask extends BaseTask<DownloadStationConfigResponse, DownloadStationConfig, DownloadStationClientParameters> {

    private static final String TAG = DownloadConfigTask.class.getName();

    public DownloadConfigTask(Context context){
        super(context);
    }

    private static final String CACHE_KEY = "downloadstation_config";

    @Override
    protected String getCacheKey() {
        return CACHE_KEY;
    }

    @Override
    public DownloadStationConfigResponse run(DownloadStationClientParameters params)  throws Exception{
            boolean expireCache = params.refreshCache();

            DownloadStationClient client = ServiceClientFactory.getClient(DownloadStationClient.class);

            Log.d(TAG, "Fetching data");
            publishProgress("Loading data from disk station");
            return client.getConfig();

    }


    @Override
    protected void onPostExecute(DownloadStationConfigResponse configResponse) {
        super.onPostExecute(configResponse);

        if(configResponse != null){
            final DownloadStationSettingsActivity act = (DownloadStationSettingsActivity)getContext();
            act.setConfig(configResponse.getData());
        }



    }
}
